package com.zwh.autoconfigure.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 登录过期拦截器
 *
 * @author monkey
 * @version 2020-06-05
 */
public class LoginInterceptor implements HandlerInterceptor {
    private static String encoding = "UTF-8";
    private static final String contentType = "application/json; charset=" + encoding;
    private static final String contentTypeForIE = "text/html; charset=" + encoding;
    private boolean forIE = false;
    private static final String TOKEN = "access_token";
    private static final String USER = "user";


    /**
     * 重写拦截器逻辑
     * TODO 拦截未登录用户请求
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        //如果不是映射到方法，直接通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

//		//根据token获取到user
//		UserDto userDto = CommonUserUtils.getUser();
//		if (userDto != null) {
//			//将user 添加到 request中，以便后续操作获取user
//			request.setAttribute(USER, userDto);
//			return true;
//		} else {
//			// throw new OperationException("用户登录过期，请重新登陆", Integer.parseInt(ReturnStatus.LOGIN_TIMEOUT));
//			ReturnObject data = ReturnObject.fail("用户未登录或登录已过期，请重新登陆！");
//			data.setStatus(ReturnStatus.LOGIN_TIMEOUT);
//			String jsonText = JSONObject.toJSONString(data);
//			PrintWriter writer = null;
//			try {
//				response.setHeader("Pragma", "no-cache");	// HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache
//				response.setHeader("Cache-Control", "no-cache");
//				response.setDateHeader("Expires", 0);
//				//forIE ? contentTypeForIE : contentType
//				response.setContentType(isIE(request)? contentTypeForIE : contentType);
//				writer = response.getWriter();
//				writer.write(jsonText);
//				writer.flush();
//			} catch (IOException e) {
//				throw new Exception(e);
//			}
//			return false;
//		}


//		HandlerMethod handlerMethod= ((HandlerMethod) handler);
//		Method method=handlerMethod.getMethod();

        //获取方法上的TokenValid注解
//		TokenValid tokenValid = method.getAnnotation(TokenValid.class);
//		//如果方法上有tokenValid注解
//		if (tokenValid != null) {
//			//获取请求上传的token信息
//			String token = request.getHeader("TOKEN");
//			//TODO 通过 token 进行校验
//
//
//		}
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
    }

    private boolean isIE(HttpServletRequest request) {
        String agent = request.getHeader("User-Agent").toLowerCase();
        if (agent.indexOf("msie") > 0) {
            return true;
        }
        if (agent.indexOf("gecko") > 0 && agent.indexOf("rv:11") > 0) {
            return true;
        }
        return false;
    }

}
